//
//  MainViewController.swift
//  weibo
//
//  Created by Fosen on 15/11/26.
//  Copyright © 2015年 fosen. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    //target action 直接使用OC机制    方法选择器相关的机制 都是OC机制  为了让swift语法能够兼容oc方法的选择器  需要在前面添加@objc
    //不加private 成为私有方法 不会崩溃 
    @objc private func composeBtnDidClick(){
    
        print(__FUNCTION__)
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //将系统的tabbar 替换成MainTabbar 就能完成自定义
        //系统的tabbar是只读属性  不能够直接操作
        let mainTabBar = MainTabBar()
        
        //给中间按钮添加点击事件
        mainTabBar.composeBtn.addTarget(self, action: "composeBtnDidClick", forControlEvents: .TouchUpInside)
        
        //通过KVC机制 动态的修改 tabbar的身份
        setValue(mainTabBar, forKey: "tabBar")   //就是把mainTabBar赋值给键值为 tabbar所对用的tabbar
        
//        print(mainTabBar.classForCoder)  //classForCoder 可打印对象的类的字符串
        
        addChildViewControllers()
    }

    
    private func addChildViewControllers(){
        
        self.tabBar.tintColor = themeColor
        addChildViewController(HomeViewController(), title: "首页", imagedName: "tabbar_home")
        addChildViewController(MassageViewController(), title: "消息", imagedName: "tabbar_message_center")
        addChildViewController(DiscoverViewController(), title: "发现", imagedName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imagedName: "tabbar_profile")

        
    }
    
    
    private func addChildViewController(vc: UIViewController,title :String,imagedName :String) {
        
        let nav = UINavigationController (rootViewController: vc)
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imagedName)
//        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_home_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//                home.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName :UIColor.orangeColor()], forState: .Selected)
        
        addChildViewController(nav)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
